
Module debugModule
    '// tests for authentication and provides the Authenticate screen. It jams. This project MUST be obfuscated or the whole thing is useless.
    '// and that sucks for all the work I've done in the last... 5 fricken years.
    Private s() As Char
    Private seed As Integer = 25923
    Private randy As Random
    Private txrfm As Integer = 100

    Private Sub FillS()
        Dim print(26) As Byte

        Dim b As Integer
        For i As Integer = 65 To 90
            print(b) = i
            b += 1
        Next

        s = System.Text.ASCIIEncoding.ASCII.GetChars(print)

    End Sub

    'Private Function HackFirstGroup(ByVal keyVal As Integer) As String
    '    Dim b(5) As Byte
    '    For i As Byte = 0 To 5
    '        b(i) = randy.Next(0, s.GetUpperBound(0))
    '    Next
    '    Dim c() As Char = {s(b(0)), s(b(1)), s(b(2)), s(b(3)), s(b(4)), s(b(5))}
    '    Return c
    'End Function

    'Private Function GenFirstGroup(ByRef keyVal As Integer) As String
    '    '// the first group is 6 letters adding to a total value of 60
    '    Dim b(5) As Byte
    '    Dim v As Integer

    '    For i As Byte = 0 To 4
    '        v = keyVal - (b(0) + b(1) + b(2) + b(3) + b(4) + b(5))
    '        If v > 25 Then v = 25
    '        If v < 0 Then v = 0

    '        b(i) = randy.Next(0, v)
    '    Next
    '    '// sanity check
    '    v = keyVal - (b(0) + b(1) + b(2) + b(3) + b(4) + b(5))
    '    If v > 25 Then
    '        '// this one is bad
    '        Throw New InvalidExpressionException
    '    End If

    '    '// if not, then set the final bit
    '    b(5) = v

    '    '// build it, return it!
    '    Dim c() As Char = {s(b(0)), s(b(1)), s(b(2)), s(b(3)), s(b(4)), s(b(5))}
    '    Return c
    'End Function

    'Private Function HackSecondGroup(ByVal keyval As Integer) As String
    '    Dim b(3) As Byte
    '    Dim v As Integer = keyval

    '    For i As Byte = 0 To b.GetUpperBound(0)
    '        b(i) = randy.Next(0, s.GetUpperBound(0))
    '    Next

    '    Dim c() As Char = {s(b(0)), s(b(1)), s(b(2)), s(b(3))}
    '    Return c
    'End Function

    'Private Function GenSecondGroup(ByVal keyval As Integer) As String
    '    '// the second group is 4 letters to a total value of 48
    '    Dim b(3) As Byte
    '    Dim v As Integer

    '    For i As Byte = 0 To 2
    '        v = keyval - (b(0) + b(1) + b(2))
    '        b(i) = randy.Next(0, 25)
    '    Next

    '    v = keyval - (b(0) + b(1) + b(2))
    '    If v > 25 Then
    '        Throw New InvalidExpressionException
    '    End If

    '    b(3) = v

    '    Dim c() As Char = {s(b(0)), s(b(1)), s(b(2)), s(b(3))}
    '    Return c

    'End Function

    Friend Function VerifyDebugOutput(ByRef opString() As Char) As Integer
        FillS()
        Dim b1() As Char = {opString(0), opString(1), opString(2), opString(3), opString(4), opString(5)} '// 85
        Dim b2() As Char = {opString(7), opString(8), opString(9), opString(10)} '// 65
        Dim b3() As Char = {opString(12), opString(13), opString(14), opString(15)} '// 45
        Dim b4() As Char = {opString(17), opString(18), opString(19), opString(20), opString(21), opString(22)} '// 65

        Try

            Dim v As Integer
            For i As Integer = b1.GetLowerBound(0) To b4.GetUpperBound(0)
                v += RVL(b1(i))
            Next
            If Not v.Equals(txrfm - (b1.GetUpperBound(0) + b2.GetUpperBound(0) + b3.GetUpperBound(0) + b4.GetUpperBound(0)) + 1) Then
                Return v
            End If

            v = New Integer
            For i As Integer = b4.GetLowerBound(0) To b3.GetUpperBound(0)
                v += RVL(b2(i))
            Next
            If Not v.Equals(txrfm - (b1.GetUpperBound(0) + b2.GetUpperBound(0) + b3.GetUpperBound(0) + b4.GetUpperBound(0) + 19)) Then
                Return v
            End If

            v = New Integer
            For i As Integer = b4.GetLowerBound(0) To b3.GetUpperBound(0)
                v += RVL(b3(i))
            Next
            If Not v.Equals(txrfm - (b1.GetUpperBound(0) + b2.GetUpperBound(0) + b3.GetUpperBound(0) + b4.GetUpperBound(0) + 39)) Then
                Return v
            End If

            v = New Integer
            For i As Integer = b4.GetLowerBound(0) To b1.GetUpperBound(0)
                v += RVL(b4(i))
            Next
            If Not v.Equals(txrfm - (b1.GetUpperBound(0) + b2.GetUpperBound(0) + b3.GetUpperBound(0) + b4.GetUpperBound(0) + 19)) Then
                Return v
            End If

            Return 200

        Catch ex As Exception
            Return False
        End Try

    End Function

    Private Function RVL(ByVal c As Char) As Integer

        For i As Integer = s.GetLowerBound(0) To s.GetUpperBound(0)
            If s(i).Equals(c) Then Return i
        Next

        Return 0

    End Function

End Module